/**
 * 
 */
package io.dora.spring.parser;

import io.dora.conf.spring.ProviderBean;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

/**
 * provider节点处理类
 * 
 * @author zhaohongbo
 *
 */
public class ProviderDefinitionParser implements BeanDefinitionParser {

	@Override
	public BeanDefinition parse(Element element, ParserContext parserContext) {
		RootBeanDefinition def = new RootBeanDefinition();

	    // 设置Bean Class
	    def.setBeanClass(ProviderBean.class);
	    def.setLazyInit(false);

	    // 注册属性
	    String id = element.getAttribute("id");
	    String interfaceName = element.getAttribute("interface");
	    String group = element.getAttribute("group");
	    String refBeanId = element.getAttribute("ref");
	    
	    def.getPropertyValues().addPropertyValue("id", id);
	    def.getPropertyValues().addPropertyValue("group", group);
	    def.getPropertyValues().addPropertyValue("refBeanId", refBeanId);
	    
	    try {
			Class<?> clazz = Class.forName(interfaceName);
			def.getPropertyValues().addPropertyValue("interfaceClass", clazz);
			
		} catch (ClassNotFoundException e) {
			try {
				throw new ClassNotFoundException();
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}
		}
		parserContext.getRegistry().registerBeanDefinition(id, def);
	    return def;
	}

}
